﻿#region Using directives

using System;
using System.Drawing.Design;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.Tools.OrganizationChart.MembersClasses;
using System.Windows.Forms;
using MCMLXVII.Tools.OrganizationChart.SmartEditors;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    #region String Translation
    public class StringTranslations
    {
        private string _language;
        private string _translation;
        public StringTranslations(string Languaje, string Translation)
        {
            _language = Languaje;
            _translation = Translation;

        }

        public string Language
        {
            get { return _language; }
        }

        public string Translation
        {
            get { return _translation; }
            set { _translation = value; }
        }

    }

    public class TranslationProperty : PropertyDescriptor
    {
        private string _LanguageID;
        private string _LanguageName;
        public TranslationProperty( string LanguageID, string LanguageName, Attribute[] attrs): base(LanguageID, attrs )
        { 
            _LanguageID = LanguageID;
            _LanguageName = LanguageName;
        }

        public override string Description
        {
            get { return _LanguageName ; }
        }

        public override string DisplayName
        {
            get { return _LanguageID ; }
        }


        public override object GetValue(object component)
        {
            return ((StringTranslationsProperty)component).GetValue(_LanguageID);

        }


        public override void SetValue(object component, object value)
        {
            ((StringTranslationsProperty)component).SetValue(_LanguageID, value.ToString());
        }

        public override Type ComponentType
        {
            get { throw new NotImplementedException(); }
        }


        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type PropertyType
        {
            get { return typeof(System.String); }
        }

        public override bool CanResetValue(object component)
        {
            return false;
        }


        public override void ResetValue(object component)
        {
            throw new NotImplementedException();
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }

    }

    public class TranslationsConverter : ExpandableObjectConverter 
    {

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override  PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            Dictionary<string,string> Langs = MemberBase.LanguageList();
            PropertyDescriptor[] Props = new PropertyDescriptor[Langs.Count];
            int i = 0;
            foreach(string key in Langs.Keys)
            {
                Props[i] = new TranslationProperty(key, Langs[key], null);
                i++;
            }
            
            return new PropertyDescriptorCollection(Props);
        }


    }

    public class StringTranslationsProperty
    {
        private  IList<StringTranslations> _NameTranslated;
        public StringTranslationsProperty()
        {
            _NameTranslated = new System.Collections.Generic.List<StringTranslations>();
        }
        public StringTranslationsProperty(IList<StringTranslations> col)
        {
            _NameTranslated = col;
        }
        public void Add(StringTranslations st)
        {
            _NameTranslated.Add(st);
        }
        public IList<StringTranslations> Collection
        {
            get { return _NameTranslated; }
        }

        public string GetValue(string key)
        { 
            foreach(StringTranslations stx in _NameTranslated)
            {  if (stx.Language==key)
                return stx.Translation;
            
            }
            return "";
        }

        public void SetValue(string key, string value)
        {
            bool sw=false;
            foreach (StringTranslations stx in _NameTranslated)
            {
                if (stx.Language == key)
                {
                    stx.Translation = value;
                    sw = true;
                }
            }
            if (!sw  && value != "")
            {
                _NameTranslated.Add(new StringTranslations(key, value));
                
            }
        }


        public override string ToString()
        {
            return "("+Properties.Resources.Translations_DisplayName+")" ;
        }


    }
    #endregion

    #region Directory Service Editor
    public class DirectoryServiceEditor : UITypeEditor
    {
        public DirectoryServiceEditor()
        { }


        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {      
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override bool IsDropDownResizable
        {
            get { return false; }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            frmDirectoryService frmDialog = new frmDirectoryService();
            frmDialog.BaseValue = (string)value;
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(); //Program.frm);
            if (res == System.Windows.Forms.DialogResult.Cancel)
                return value;
            else
                return frmDialog.BaseValue;
        }
    }

    #endregion
    public class NodeEditorHelper
    {
        private int _FatherNodeID;

        private int _ID;
        [LocCategory("Core"), ReadOnly(true), LocDescription("Identification_Description"), LocDisplayName("Identification_DisplayName")]
        public int ID
        {
            get
            {
                return _ID;
            }

        }

        private string _Name;
        [LocCategory("Core"), ReadOnly(false), LocDescription("Name_Description"), LocDisplayName("Name_DisplayName")]
        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                _Name = value;
            }
        }
        private msgMembershipInfo _OwnerID;
        [LocCategory("Misc"), LocDescription("Owner_Description"), LocDisplayName("Owner_DisplayName"), Editor(typeof(OwnerEditor), typeof(UITypeEditor))]
        public msgMembershipInfo OwnerID
        {
            get
            {
                return _OwnerID;
            }

            set
            {
                _OwnerID = value;
            }
        }


        StringTranslationsProperty  _NameTranslatedProperty;
        [LocCategory("Core"), ReadOnly(false), LocDescription("Translations_Description"), LocDisplayName("Translations_DisplayName"), TypeConverter(typeof(TranslationsConverter)) ]
        public StringTranslationsProperty NameTranslated
        {
            get
            {
                return _NameTranslatedProperty;
            }

            set
            {
                _NameTranslatedProperty = value;
            }
        }

        string _DirectoryServiceConfigInfo;
        [LocCategory("Directory"), ReadOnly(false), LocDescription("Directory_Description"), LocDisplayName("Directory_DisplayName"), Editor(typeof(DirectoryServiceEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string DirectoryServiceConfigInfo
        {
            get
            {
                return _DirectoryServiceConfigInfo;
            }

            set
            {
                _DirectoryServiceConfigInfo = value;
            }
        }

        public NodeEditorHelper(OrganizationTreeNode Node)
        {
            _FatherNodeID = Node.BaseOrganizationNodeInfo.FatherID; 
            _ID = Node.BaseOrganizationNodeInfo.NodeID;
            _Name = Node.BaseOrganizationNodeInfo.Name;
            _NameTranslatedProperty = new StringTranslationsProperty();
            _OwnerID = MembersClasses.MemberBase.FindMemberInfo(Node.BaseOrganizationNodeInfo.OwnerID);

            if (Node.BaseOrganizationNodeInfo.Translations != null)
            {
                foreach (NodeLanguageTranslations nod in Node.BaseOrganizationNodeInfo.Translations)
                {
                    _NameTranslatedProperty.Add(new StringTranslations(nod.Language, nod.Name));
                }
            }
            _DirectoryServiceConfigInfo = Node.BaseOrganizationNodeInfo.DirectoryConfigInfo;
        }
         
        public ucSmartBaseEditor GetEditControl(bool EditInGrid)
        {
            if (EditInGrid)
                return new ucSmartGridEditor(this, _FatherNodeID);
            else
                return new ucSmartNodeEditor(this, _FatherNodeID);            
        }

    }
}
